package com.lepu.app.usercenter.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lepu.app.usercenter.FeedBackActivity;
import com.lepu.app.usercenter.FeedBackMainActivity;
import com.lepu.app.usercenter.bean.BeanFeedback;
import com.lepu.app.widget.ListViewForScrollview;
import com.lepu.pasm.R;

public class AdapterFeedback extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private FeedBackMainActivity mContext = null;
	private ArrayList<BeanFeedback> mArrayList = null;

	public AdapterFeedback(FeedBackMainActivity context, ArrayList<BeanFeedback> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.usercenter_feedback_main_activity_listitem, null);

			holder = new ViewHolder();
			
			LinearLayout askLayout = (LinearLayout) convertView.findViewById(R.id.askLayout);
			TextView askTimeTextView = (TextView) convertView.findViewById(R.id.askTimeTextView);
			TextView askContentTextView = (TextView) convertView.findViewById(R.id.askContentTextView);
			TextView noAnswerTextView = (TextView) convertView.findViewById(R.id.noAnswerTextView);
			
			LinearLayout answerLayout = (LinearLayout) convertView.findViewById(R.id.answerLayout);
			ListViewForScrollview replayListView = (ListViewForScrollview) convertView.findViewById(R.id.replayListView);
			LinearLayout answerTipLayout = (LinearLayout) convertView.findViewById(R.id.answerTipLayout);
			ImageButton goFeedbackImageButton = (ImageButton) convertView.findViewById(R.id.goFeedbackImageButton);
			
			holder.askLayout = askLayout;
			holder.askTimeTextView = askTimeTextView;
			holder.askContentTextView = askContentTextView;
			holder.noAnswerTextView = noAnswerTextView;
			
			holder.answerLayout = answerLayout;
			holder.replayListView = replayListView;
			holder.answerTipLayout = answerTipLayout;
			holder.goFeedbackImageButton = goFeedbackImageButton;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		final BeanFeedback item = mArrayList.get(position);
		
		//ask
		holder.askLayout.setVisibility(View.VISIBLE);
		holder.askTimeTextView.setText(String.format("%s", item.RecordCreateTime));
		holder.askContentTextView.setText(item.AdviceContent);
		
		//test
//		item.ReplyList = new ArrayList<BeanFeedbackReply>();
//		for(int i=0;i<2;i++)
//		{
//			BeanFeedbackReply beanFeedbackReply = new BeanFeedbackReply();
//			beanFeedbackReply.ReplyUserID = "222";
//			beanFeedbackReply.ReplyTime = "222";
//			beanFeedbackReply.ReplyContent = "ReplyContent";
//			item.ReplyList.add(beanFeedbackReply);
//		}
//		
		//answer
		if(item.ReplyList == null || item.ReplyList.size() == 0)
		{
			holder.answerLayout.setVisibility(View.GONE);
			holder.noAnswerTextView.setVisibility(View.VISIBLE);
		}else{
			holder.noAnswerTextView.setVisibility(View.GONE);
			holder.answerLayout.setVisibility(View.VISIBLE);
			holder.answerTipLayout.setVisibility(View.VISIBLE);
			holder.goFeedbackImageButton.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mContext, FeedBackActivity.class);
					intent.putExtra("AdviceID", item.AdviceID);
					mContext.startActivity(intent, true);
				}
			});
			
			//replay data list view
			AdapterFeedbackReply adapterFeedbackReply = new AdapterFeedbackReply(mContext, item.ReplyList,item.UserID);
			holder.replayListView.setAdapter(adapterFeedbackReply);
		}

		return convertView;
	}

	private static class ViewHolder {
		LinearLayout askLayout;
		TextView askTimeTextView;
		TextView askContentTextView;
		TextView noAnswerTextView;
		
		LinearLayout answerLayout;
		ListViewForScrollview replayListView;
		LinearLayout answerTipLayout;
		ImageButton goFeedbackImageButton;
	}
}
